<?php


class dbaUser {

  // Zeit seit dem letzten Request ab deren berschreiten ein User offline ist/als offline gilt
  var $offtime = 3600;

  function dbaUser (&$dba)
  {
    $this->dba = &$dba;
  }

  function add ($name,$long_name,$email,$descr,$pwd,$group,$xics_login,$type = 'user')
  {
    if ($xics_login=='')
      $xics_login = 'N';
    $this->dba->query("insert into ".$this->dba->tableforwarder
        ."_User (name,long_name,email,descr,pwd,type,xics_login) values ("
        .$this->dba->genQuotedFieldStr(array($name,$long_name,$email,$descr,$pwd,$type,$xics_login)).");");
    $id = $this->dba->insert_id();
    if ($type == 'user' && $id)
        $this->saveMenuSettings($id, "1;80;81;79");
    if ($group)
    {
      return $this->dba->query("insert into ".$this->dba->tableforwarder."_GroupUser (user_id,group_id) values ('$id','$group');");
    }
  }

  function isMemberByName ($user, $group)
  {
    $this->dba->query("select * from ".$this->dba->tableforwarder."_GroupUser GU, ".$this->dba->tableforwarder."_User U, ".$this->dba->tableforwarder
        ."_User G where GU.user_id=U.id AND GU.group_id=G.id and U.name=".$this->dba->quote($user)
        ." and U.type='user' and G.name=".$this->dba->quote($group)." and G.type='group';");
    if ($this->dba->num_rows()) return true;
  }

  function authenticate ($user, $password, $group = '')
  {
      if ($group)
      {
        $this->dba->query("select * from ".$this->dba->tableforwarder."_GroupUser GU, ".$this->dba->tableforwarder."_User U, ".$this->dba->tableforwarder
            ."_User G where GU.user_id=U.id AND GU.group_id=G.id and U.name=".$this->dba->quote($user)
            ." and U.type='user' and U.pwd=".$this->dba->quote($password)." and G.name=".$this->dba->quote($group)." and G.type='group';");
      } else {
        $this->dba->query("select * from ".$this->dba->tableforwarder."_User where name=".$this->dba->quote($name)." AND pwd=".$this->dba->quote($pwd)." AND type='user' AND xics_login='Y';");
      }  
      if ($this->dba->num_rows()) return true;
  }

  function addMembership ($user_id,$group_id)
  {
    if (!$this->isMember($user_id,$group_id))
      return $this->dba->query("insert into ".$this->dba->tableforwarder."_GroupUser (user_id,group_id) values ('$user_id','$group_id');");
  }

  function removeMembership ($user_id,$group_id)
  {
    return $this->dba->query("delete from ".$this->dba->tableforwarder."_GroupUser where user_id='$user_id' AND group_id='$group_id';");
  }

  function removeAllMemberships ($group_id)
  {
    return $this->dba->query("delete from ".$this->dba->tableforwarder."_GroupUser where group_id='$group_id';");
  }

  function isMember($user_id,$group_id)
  {
    $this->dba->query("select * from ".$this->dba->tableforwarder."_GroupUser where user_id='$user_id' AND group_id='$group_id';");
    if ($this->dba->num_rows()) return true;
  }

  function del ($id)
  {
    $this->dba->query("delete from ".$this->dba->tableforwarder."_GroupUser where user_id='$id' or group_id='$id';");
    return $this->dba->query("delete from ".$this->dba->tableforwarder."_User where id='$id';");
  }

  function update ($id,$name,$long_name,$email,$descr,$pwd,$xics_login,$type='user')
  {
    if ($xics_login=='')
      $xics_login = 'N';
    return $this->dba->query("update ".$this->dba->tableforwarder."_User set "
        .$this->dba->genUpdateFieldStr(array('xics_login'=>$xics_login, 'name'=>$name, 'long_name'=>$long_name, 'email'=>$email, 'descr'=>$descr, 'pwd'=>$pwd, 'type'=>$type))." where id=$id;");
  }

  function get ($id)
  {
   $this->dba->query("select * from ".$this->dba->tableforwarder."_User where id='$id';");
   return $this->dba->fetch_row_array();
  }

  function getEmail ($id)
  {
   $this->dba->query("select email from ".$this->dba->tableforwarder."_User where id='$id';");
   $usr = $this->dba->fetch_row_array();
   return $usr['email'];
  }

  function getall ($type = 'user')
  {
   $this->dba->query("select * from ".$this->dba->tableforwarder."_User where type="
        .$this->dba->quote($type)." order by name;");
   return $this->dba->fetch_rows_array();
  }

  function getByGroup ($group)
  {
   $this->dba->query("select * from ".$this->dba->tableforwarder."_User left join ".$this->dba->tableforwarder."_GroupUser on (id=user_id) where type='user' AND group_id = '$group' order by name;");
   return $this->dba->fetch_rows_array();
  }

  function getGroupsByUser ($user)
  {
   $this->dba->query("select * from ".$this->dba->tableforwarder."_User left join ".$this->dba->tableforwarder."_GroupUser on (id=group_id) where type='group' AND user_id = '$user' order by name;");
   return $this->dba->fetch_rows_array();
  }

  function getByGroupFields ($group,$fields)
  {
   $this->dba->query("select ".$this->dba->genFieldStr($fields)." from ".$this->dba->tableforwarder."_User,".$this->dba->tableforwarder."_GroupUser where type='user' AND group_id = '$group' AND id=user_id order by name;");
   return $this->dba->fetch_rows_array();
  }

  function getallFields ($type = 'user',$fields)
  {
   $this->dba->query("select ".$this->dba->genFieldStr($fields)." from ".$this->dba->tableforwarder."_User where type="
        .$this->dba->quote($type)." order by name;");
   return $this->dba->fetch_rows_array();
  }

  function getallStatus ()
  {
   $this->dba->query("select * from ".$this->dba->tableforwarder."_User where type='user' order by name;");
   $users = $this->dba->fetch_rows_array();
   $this->dba->query("select * from ".$this->dba->tableforwarder."_onlineUser order by name;");
   $online = $this->dba->fetch_rows_array();
   for ($i=0;$i<count($online);$i++)
   {
     $onbyuid[$online[$i]['uid']] = &$online[$i];
   }
   for ($i=0;$i<count($users);$i++)
   {
     if ($onbyuid[$users[$i]['id']] && ($onbyuid[$users[$i]['id']]['time']+$this->offtime >= time())) $users[$i]['online'] = true;
   }
   return $users;
  }

  function getStatus ($uid)
  {
   $this->dba->query("select * from ".$this->dba->tableforwarder."_User where id = $uid;");
   $user = $this->dba->fetch_row_array();
   $this->dba->query("select * from ".$this->dba->tableforwarder."_onlineUser  where uid = $uid;");
   $online = $this->dba->fetch_row_array();
   if ($online && ($online['time']+$this->offtime >= time())) $user['online'] = true;
   return $user;
  }

  function nameExists($name,$type = 'user',$id = '')
  {
   $str = '';
   if ($id)
     $str = " AND id != $id";
   $this->dba->query("select * from ".$this->dba->tableforwarder."_User where name=".$this->dba->quote($name)
        ." AND type = ".$this->dba->quote($type)."$str;");
   if ($this->dba->num_rows() > 0) return true;
  }


  function saveMenuSettings($id,$settings)
  {
   return $this->dba->query("update ".$this->dba->tableforwarder."_User set menusettings="
    .$this->dba->quote($settings)." where id='$id';");
  }


}

?>